package cn.iocoder.yudao.module.forward.controller.admin.datasource;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.datasource.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasource.DataSourceDO;
import cn.iocoder.yudao.module.forward.service.datasource.DataSourceService;

@Tag(name = "管理后台 - 信息源")
@RestController
@RequestMapping("/forward/data-source")
@Validated
public class DataSourceController {

    @Resource
    private DataSourceService dataSourceService;

    @PostMapping("/create")
    @Operation(summary = "创建信息源")
    @PreAuthorize("@ss.hasPermission('forward:data-source:create')")
    public CommonResult<Integer> createDataSource(@Valid @RequestBody DataSourceSaveReqVO createReqVO) {
        return success(dataSourceService.createDataSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新信息源")
    @PreAuthorize("@ss.hasPermission('forward:data-source:update')")
    public CommonResult<Boolean> updateDataSource(@Valid @RequestBody DataSourceSaveReqVO updateReqVO) {
        dataSourceService.updateDataSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除信息源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:data-source:delete')")
    public CommonResult<Boolean> deleteDataSource(@RequestParam("id") Integer id) {
        dataSourceService.deleteDataSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得信息源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:data-source:query')")
    public CommonResult<DataSourceRespVO> getDataSource(@RequestParam("id") Integer id) {
        DataSourceDO dataSource = dataSourceService.getDataSource(id);
        return success(BeanUtils.toBean(dataSource, DataSourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得信息源分页")
    @PreAuthorize("@ss.hasPermission('forward:data-source:query')")
    public CommonResult<PageResult<DataSourceRespVO>> getDataSourcePage(@Valid DataSourcePageReqVO pageReqVO) {
        PageResult<DataSourceDO> pageResult = dataSourceService.getDataSourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataSourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出信息源 Excel")
    @PreAuthorize("@ss.hasPermission('forward:data-source:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataSourceExcel(@Valid DataSourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataSourceDO> list = dataSourceService.getDataSourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "信息源.xls", "数据", DataSourceRespVO.class,
                        BeanUtils.toBean(list, DataSourceRespVO.class));
    }

}